﻿//-------------------------------------------------------------------------------------
// <copyright file="Exist.cs" company="Microsoft">
//      Copyright (c) 2003 Microsoft Corporation. All rights reserved.
//      THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
//      EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
//      WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// </copyright>
// <summary>
//      Checks if an Orchestration exists in an assembly in BizTalk Server.
// </summary>  
//-------------------------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.BizTalk2004.Orchestration
{
    #region Using directives

    using System;
    using Microsoft.Build.Framework;
    using Microsoft.Sdc.Tasks.BizTalk2004.Configuration;
    using Microsoft.Sdc.Tasks;

    #endregion


    #region Class Comments
    /// <summary>
    ///   Checks if an Orchestration exists in an assembly in BizTalk Server.
    /// </summary>
    /// <remarks>
    /// <code>
    /// <![CDATA[<BizTalk2004.Orchestration.Exists AssemblyName = "assemblyname" Name="name" Server="server" Database="database" />]]>
    /// </code>
    /// <para>where:</para>
    /// <para><i>assemblyName (Required)</i></para>
    /// <para>
    /// The name of the assembly that contains the orchestration.
    /// </para>
    /// <para><i>name (Required)</i></para>
    /// <para>
    /// The name of the orchestration to check.
    /// </para>
    /// <para><i>server</i></para>
    /// <para>
    /// The logical name of the server containing the BizTalk management database. If neither this nor the
    /// database name are specified then the installation will default to the local installation.
    /// </para>
    /// <para><i>database</i></para>
    /// <para>
    /// The name of the BizTalk management database. If neither this nor the server name are specified 
    /// then the installation will default to the local installation.
    /// </para>
    /// <para><i>username</i></para>
    /// <para>
    /// The username to use to connect to the database if using SQL Server security. The default is to
    /// connect using Windows Integrated security.
    /// </para>
    /// <para><i>password</i></para>
    /// <para>
    /// The password to use to connect to the database if using SQL Server security. The default is to
    /// connect using Windows Integrated security.
    /// </para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///            <BizTalk2004.Orchestration.Exists
    ///                AssemblyName="EAIOrchestrations, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a0cf43ab166707e8"
    ///                Name="EAIOrchestrations.EAIProcess" />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    #endregion
    public class Exists : BizTalk2004TaskBase
    {
        #region Member Variables

        private string assemblyName;
        private string name;
        private bool orchestrationExists;

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new instance of the Exist task.
        /// </summary>
        public Exists()
        {
        }

        #endregion

        #region Properties


        /// <summary>
        /// Gets or sets the name of the Assembly that contains the Orchestration.
        /// </summary>
        /// <value>
        /// The name of the Assembly that contains the Orchestration.
        /// </value>
        [Required]
        public string AssemblyName
        {
            get
            {
                return this.assemblyName;
            }
            set
            {
                this.assemblyName = value;
            }

        }


        /// <summary>
        /// Gets or sets the name of the Orchestration.
        /// </summary>
        /// <value>
        /// The name of the Orchestration.
        /// </value>
        [Required]
        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <value></value>
        [Output]
        public bool OrchestrationExists
        {
            get { return this.orchestrationExists; }
            protected set { this.orchestrationExists = value; }
        }


        #endregion

        #region Methods

        /// <summary>
        /// Executes the task.
        /// </summary>
        protected override void InternalExecute()
        {
            BizTalkInstallation installation = this.GetInstallation();
            if (installation.DeployedAssemblies.Contains(this.assemblyName))
            {
                this.orchestrationExists = Microsoft.Sdc.Tasks.BizTalk2004.Configuration.Orchestration.Exists(installation.DeployedAssemblies[this.assemblyName], this.name);
            }
            else
            {
                this.orchestrationExists = false;
            }
        }
        #endregion

    }
}

